package dao.impl;

import dao.RecordDAO;
import entity.Expense;
import entity.Income;
import entity.Record;
import entity.Budget;
import util.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class RecordDAOImpl implements RecordDAO {
    
    public boolean addIncome(Income income) {
        String sql ="INSERT INTO record (date, amount, category, remark) VALUES ('"+income.getDate()+"',"+income.getAmount()+",'"+income.getCategory()+"','"+income.getRemark()+"');";
        DBUtil dbUtil=new DBUtil();
        int num=dbUtil.executeUpdate(sql);
        if (num!=1){
            return true;
        }
        return false;
    }

    @Override
    public boolean addExpense(Expense expense) {
        String sql ="INSERT INTO record (date, amount, category, remark) VALUES ('"+expense.getDate()+"',"+-expense.getAmount()+",'"+expense.getCategory()+"','"+expense.getRemark()+"');";
        DBUtil dbUtil=new DBUtil();
        int num=dbUtil.executeUpdate(sql);
        if (num!=1){
            return true;
        }
        return false;
    }

    @Override
    public ArrayList<Record> displayAllRecord() throws SQLException {
        String sql ="SELECT * FROM record;";
        DBUtil dbUtil=new DBUtil();
        ResultSet rs=dbUtil.executeQuery(sql);
        ArrayList<Record> recordList=new ArrayList<>();
        while (rs.next()) {
            // 假设Record类有相应的构造函数或者setter方法来设置属性
            Record record = new Record();
            record.setDate(rs.getString("date"));
            record.setAmount(rs.getDouble("amount"));
            record.setCategory(rs.getString("category"));
            record.setRemark(rs.getString("remark"));
            recordList.add(record);
        }

        return recordList;
    }

    @Override
    public ArrayList<Record> queryRecord(String keyword) throws SQLException {
        String sql ="SELECT * FROM record WHERE date LIKE '%"+keyword+"%' OR amount LIKE '%"+keyword+"%' OR category LIKE '%"+keyword+"%' OR remark LIKE '%"+keyword+"%';";
        DBUtil dbUtil=new DBUtil();
        ResultSet rs=dbUtil.executeQuery(sql);
        ArrayList<Record> recordList=new ArrayList<>();
        while (rs.next()) {
            // 假设Record类有相应的构造函数或者setter方法来设置属性
            Record record = new Record();
            record.setDate(rs.getString("date"));
            record.setAmount(rs.getDouble("amount"));
            record.setCategory(rs.getString("category"));
            record.setRemark(rs.getString("remark"));
            recordList.add(record);
        }
        return recordList;
    }


    public void setMonthlyBudget(Budget budget) {
        String sql="INSERT INTO Budget(amount,month) VALUES('"+ budget.getAmount() +"','"+budget.getMonth()+"')";
    }
}